#ifndef __MEMORY_H__
#define __MEMORY_H__

#include "common.h"//引入公共头文件

#define HW_MEM_SIZE (128 * 1024 * 1024)//hardware memory size，定义了内存的大小

extern uint8_t *hw_mem;//使用ctags追踪得到hw_mem指向dram数组的起始地址，dram代表了DRAM，相当于可以使用这个指针进行内存读写操作

/* convert the hardware address in the test program to virtual address in NEMU */
#define hwa_to_va(p) ((void *)(hw_mem + (unsigned)p))
/* convert the virtual address in NEMU to hardware address in the test program */
#define va_to_hwa(p) ((hwaddr_t)((void *)p - (void *)hw_mem))
//上面是地址转换的宏，用于硬件地址和虚拟地址的相互转换
#define hw_rw(addr, type) *(type *)({\
	Assert(addr < HW_MEM_SIZE, "physical address(0x%08x) is out of bound", addr); \
	hwa_to_va(addr); \
})//首先检测地址是否在内存内，否则触发后面的断言，然后进行虚拟地址转换，进行读写操作

uint32_t swaddr_read(swaddr_t, size_t);
uint32_t lnaddr_read(lnaddr_t, size_t);
uint32_t hwaddr_read(hwaddr_t, size_t);
void swaddr_write(swaddr_t, size_t, uint32_t);
void lnaddr_write(lnaddr_t, size_t, uint32_t);
void hwaddr_write(hwaddr_t, size_t, uint32_t);
void init_cache();
//读取和写入数据
#endif